package com.time.auth.domain.service.impl;

import com.time.auth.common.enums.IsDeletedFlagEnum;
import com.time.auth.domain.entity.AuthRolePermissionBO;
import com.time.auth.domain.service.AuthRolePermissionDomainService;
import com.time.auth.infra.basic.entity.AuthRolePermission;
import com.time.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * 角色权限关联领域服务实现类
 *
 * @author: HuangQi
 * @date: Created in 16:35 2025/9/28
 * @description: 角色权限关联领域服务实现类
 */
@Slf4j
@Service("authRolePermissionDomainService")
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Autowired
    private AuthRolePermissionService authRolePermissionService;

    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRolePermissionDomainServiceImpl.add.bo:{}", authRolePermissionBO);
        }

        // 参数转换
        List<AuthRolePermission> rolePermissionList = new LinkedList<>();
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(authRolePermissionBO.getRoleId());
            authRolePermission.setPermissionId(permissionId);
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            rolePermissionList.add(authRolePermission);
        });

        // 调用service
        Integer count = authRolePermissionService.batchInsert(rolePermissionList);

        return count > 0;
    }
}
